package com.david.rocket.dao;

import com.david.rocket.entity.GoodsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

public interface GoodsRepository extends JpaRepository<GoodsEntity, Long> {
    //防止超卖减库存
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update GoodsEntity set num = num-1 where id=?1 and num>0")
    int reduceStock(Long id);

    //增加库存
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update GoodsEntity set num = num+1 where id=?1 and num>=0")
    int increaseStock(Long id);
}
